package com.edu.interceptor;
/*
 * @Author:princess
 * @Date 2023/10/31 10 31 19:22
 *
 */

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogInterceptor implements MethodInterceptor {
    private Object target;
    public LogInterceptor(Object o){
        this.target = o;
    }
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        long start = System.currentTimeMillis();
        Object invoke = method.invoke(target, objects);
        long end = System.currentTimeMillis();
        System.out.println("执行了"+method.getName()+"开始执行于"+dateFormat.format(new Date())+"耗时："+(end-start));
        return invoke;
    }
}
